package com.jinjiafeng.framelibrary;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.IdRes;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jinjiafeng.baselibrary.widget.navigationbar.BaseNavigationBar;

/**
 * @author jinjiafeng
 *         Time :18-4-22
 */

public class DefaultNavigationBar extends BaseNavigationBar<DefaultNavigationBar.NavigationParams> {

    private DefaultNavigationBar(NavigationParams params) {
        super(params);
    }

    @Override
    public int bindLayoutId() {
        return R.layout.titl_bar;
    }

    @Override
    public void applyView(){
        setText(R.id.tv_title,mParams.mTitle);
        setText(R.id.tv_right_text,mParams.mRightText);
        setOnClickListener(R.id.tv_right_text,mParams.mOnRightClickListener);
        setOnClickListener(R.id.iv_left_icon,mParams.mOnLeftClickListener);
    }

    private void setOnClickListener(@IdRes int id, View.OnClickListener onClickListener) {
        findViewById(id).setOnClickListener(onClickListener);
    }

    private void setText(@IdRes int id,CharSequence text) {
        TextView textView = findViewById(id);
        if(!TextUtils.isEmpty(text)){
            textView.setVisibility(View.VISIBLE);
            textView.setText(text);
        }
    }

    @SuppressWarnings("unchecked")
    private <T extends View> T findViewById(@IdRes int id){
       return (T)mParams.mViewGroup.findViewById(id);
    }

    public static class Builder extends BaseBuilder {
        private final NavigationParams mParams;

        @SuppressWarnings("WeakerAccess")
        public Builder(Context context, ViewGroup viewGroup) {
            super(context, viewGroup);
            mParams = new NavigationParams(context,viewGroup);
        }

        public Builder(Context context) {
            this(context,null);
        }

        public Builder setTitle(CharSequence text){
            mParams.mTitle = text;
            return this;
        }

        public Builder setRightText(CharSequence text){
            mParams.mRightText = text;
            return this;
        }

        public Builder setRightOnclickListener(View.OnClickListener listener){
            mParams.mOnRightClickListener = listener;
            return this;
        }

        public Builder setLeftOnclickListener(View.OnClickListener listener){
            mParams.mOnLeftClickListener = listener;
            return this;
        }

        @Override
        public DefaultNavigationBar build() {
            return new DefaultNavigationBar(mParams);
        }
    }

    static class NavigationParams extends BaseNavigationBar.BaseNavigationParams{
        CharSequence mTitle;
        private View.OnClickListener mOnRightClickListener;
        private CharSequence mRightText;
        View.OnClickListener mOnLeftClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity)mContext).finish();
            }
        };

        NavigationParams(Context context, ViewGroup viewGroup) {
            super(context, viewGroup);
        }
    }
}
